﻿using ConsoleApplication.WebAPI;
using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HM
{
    [HotUpdate]
    [Description("判断出入库流水从直接调拨下推销售出库")]
    public class DBPushCk2 : IScheduleService
    {
        public void Run(Context ctx, Schedule schedule)
        {
            try
            {
                var result = InvokeHelper.Login();
                var iResult = JObject.Parse(result)["LoginResultType"].Value<int>();
                string MATERILA = "", F_RXWU_Text2 = "", FDATE = "", DBFEnteyID = "";
                if (iResult == 1)
                {

                    string SQL0 = "/*dialect*/" + string.Format(@"select (t1.F_RXWU_Text6+t2.FNUMBER)PTDH into #E from RXWU_t_Cust100002 t
							left join RXWU_t_Cust_Entry100013 t1 on t.FID=t1.FID
							LEFT join T_BD_MATERIAL t2 on t1.FMATERIALID=t2.FMATERIALID
							where t1.F_RXWU_Text2='销售退货' and T1.F_RXWU_Text7 NOT LIKE '%抽奖%'

							select T10.FNUMBER MATERILA,T.F_RXWU_Text2,T.FDATE,t1.FEntryID DBFEnteyID into #A
                            from T_STK_STKTRANSFERIN t
                            left join T_STK_STKTRANSFERINENTRY t1 on t.fid=t1.fid
                            Left join T_BD_MATERIAL t10 on t1.FMATERIALID=t10.FMATERIALID
							left join T_STK_STKTRANSFERINENTRY_R t3 on t1.FENTRYID=t3.FENTRYID
                            where FBizType<>'DRPTRANS'
							and t1.F_RXWU_QTY2=0 and t.F_RXWU_Text2<>''

							select T1.F_RXWU_Text6,T2.FNUMBER,T1.F_RXWU_Date2,T1.FEntryId,T1.F_RXWU_Date,T1.F_RXWU_Text16 LX INTO #B  from RXWU_t_Cust100002 t
							left join RXWU_t_Cust_Entry100013 t1 on t.FID=t1.FID
							left join T_BD_MATERIAL t2 on t1.FMATERIALID=t2.FMATERIALID
							WHERE 
							T1.F_RXWU_Text6 not in(SELECT F_RXWU_Text6 FROM #E) AND
						    (T1.F_RXWU_TEXT15='AutoCheck'OR T1.F_RXWU_TEXT15='UserCheck')  AND t1.F_RXWU_Text2='销售出仓' AND t1.F_RXWU_Text16='普通订单' and T1.F_RXWU_Text7 NOT LIKE '%抽奖%'
                            AND  t1.F_RXWU_Date2>(select max(fclosedate)fclosedate from t_stk_closeprofile where fcategory = 'STK' and FORGID=100071 group by forgid)
                     

						
							select T1.F_RXWU_Text6,T2.FNUMBER,T1.F_RXWU_Date2,T1.FEntryId,T1.F_RXWU_Date,T1.F_RXWU_Text16 LX into #H from RXWU_t_Cust100002 t
							left join RXWU_t_Cust_Entry100013 t1 on t.FID=t1.FID
							left join T_BD_MATERIAL t2 on t1.FMATERIALID=t2.FMATERIALID
							left join #E t3 on t1.F_RXWU_Text6=t3.F_RXWU_Text6 and t2.FNUMBER<>t3.FNUMBER
							WHERE 
						    (T1.F_RXWU_TEXT15='AutoCheck'OR T1.F_RXWU_TEXT15='UserCheck')  AND t1.F_RXWU_Text2='销售出仓' AND t1.F_RXWU_Text16='普通订单' and T1.F_RXWU_Text7 NOT LIKE '%抽奖%'
                            AND  t1.F_RXWU_Date2>(select max(fclosedate)fclosedate from t_stk_closeprofile where fcategory = 'STK' and FORGID=100071 group by forgid)
                         

							
					        
							

							select T1.F_RXWU_Text6,T2.FNUMBER,T1.F_RXWU_Date2,T1.FEntryId,T1.F_RXWU_Date,T1.F_RXWU_Text16 LX INTO #C from RXWU_t_Cust100002 t
							left join RXWU_t_Cust_Entry100013 t1 on t.FID=t1.FID
							left join T_BD_MATERIAL t2 on t1.FMATERIALID=t2.FMATERIALID
							WHERE (t1.F_RXWU_Text16='补发订单'
                            OR t1.F_RXWU_Text16='换货订单') 
							and T1.F_RXWU_Text7 NOT LIKE '%抽奖%'
                            AND T1.F_RXWU_DATE>(select max(fclosedate)fclosedate from t_stk_closeprofile where fcategory = 'STK' and FORGID=100071 group by forgid)
                            
							
							select T1.F_RXWU_Text6,T2.FNUMBER,T1.F_RXWU_Date2,T1.FEntryId,T1.F_RXWU_Date,T1.F_RXWU_Text16 LX into #K  from RXWU_t_Cust100002 t
							left join RXWU_t_Cust_Entry100013 t1 on t.FID=t1.FID
							left join T_BD_MATERIAL t2 on t1.FMATERIALID=t2.FMATERIALID
							WHERE T1.F_RXWU_Text7  LIKE '%抽奖%'
                            AND T1.F_RXWU_DATE>(select max(fclosedate)fclosedate from t_stk_closeprofile where fcategory = 'STK' and FORGID=100071 group by forgid)
							

							select T1.F_RXWU_Text6,T2.FNUMBER,T1.F_RXWU_Date2,T1.FEntryId,T1.F_RXWU_Date,T1.F_RXWU_Text16 LX INTO #D from RXWU_t_Cust100002 t
							left join RXWU_t_Cust_Entry100013 t1 on t.FID=t1.FID
							left join T_BD_MATERIAL t2 on t1.FMATERIALID=t2.FMATERIALID
                            left join (
					        select b.F_RXWU_Text6,b.FMATERIALID,a.FDATE from RXWU_t_Cust100002 a
					        left join RXWU_t_Cust_Entry100013 b on a.FID=b.FID
					        where b.F_RXWU_Text2='销售退货' and b.F_RXWU_Text7 NOT LIKE '%抽奖%'
					        ) t11 on t1.F_RXWU_Text6=t11.F_RXWU_Text6 and t1.FMATERIALID=t11.FMATERIALID
							WHERE 
							--T1.F_RXWU_Text6 not in(SELECT * FROM #E) AND
							 (T1.F_RXWU_TEXT15='AutoCheck'OR T1.F_RXWU_TEXT15='UserCheck')  AND t1.F_RXWU_Text2='销售出仓' AND t1.F_RXWU_Text16='普通订单'
                            AND  t1.F_RXWU_Date2>(select max(fclosedate)fclosedate from t_stk_closeprofile where fcategory = 'STK' and FORGID=100071 group by forgid)
                            and t11.FDATE>=T1.F_RXWU_DATE2 and T1.F_RXWU_Text7 NOT LIKE '%抽奖%'
                           

							SELECT DISTINCT *, CONVERT(VARCHAR, F_RXWU_Date, 23)FDATE1,CONVERT(VARCHAR, F_RXWU_Date2, 23)FDATE2 FROM (
							SELECT * FROM #A T
							LEFT JOIN #B T1 ON T.F_RXWU_Text2=T1.F_RXWU_Text6 AND T.MATERILA=T1.FNUMBER AND T.FDATE=T1.F_RXWU_DATE
							WHERE T1.F_RXWU_TEXT6 IS NOT NULL
							UNION ALL
							SELECT * FROM #A T
							LEFT JOIN #C T1 ON T.F_RXWU_TEXT2=T1.F_RXWU_TEXT6 AND T.MATERILA=T1.FNUMBER AND T.FDATE=T1.F_RXWU_DATE
							WHERE T1.F_RXWU_TEXT6 IS NOT NULL
							UNION ALL
							SELECT * FROM #A T
							LEFT JOIN #D T1 ON T.F_RXWU_TEXT2=T1.F_RXWU_TEXT6 AND T.MATERILA=T1.FNUMBER AND T.FDATE=T1.F_RXWU_DATE
							WHERE T1.F_RXWU_TEXT6 IS NOT NULL
							UNION ALL
							SELECT * FROM #A T
							LEFT JOIN #H T1 ON T.F_RXWU_TEXT2=T1.F_RXWU_TEXT6 AND T.MATERILA=T1.FNUMBER AND T.FDATE=T1.F_RXWU_DATE
							WHERE T1.F_RXWU_TEXT6 IS NOT NULL and t1.PTDH IN(SELECT PTDH FROM #E)
							UNION ALL
							SELECT * FROM #A T
							LEFT JOIN #K T1 ON T.F_RXWU_TEXT2=T1.F_RXWU_TEXT6 AND T.MATERILA=T1.FNUMBER AND T.FDATE=T1.F_RXWU_DATE
							WHERE T1.F_RXWU_TEXT6 IS NOT NULL
							)T");
                    DynamicObjectCollection DynamData0 = DBUtils.ExecuteDynamicObject(ctx, SQL0);
                    if (DynamData0.Count > 0)
                    {
                        for (int i = 0; i < DynamData0.Count; i++)
                        {
                            string F_RXWU_Date2 = "", FEntryId = "", F_RXWU_Date = "", LX = "";
                            LX = Convert.ToString(DynamData0[i]["LX"]);
                            FEntryId = Convert.ToString(DynamData0[i]["FEntryId"]);
                            if(DynamData0[i]["FDATE2"] !=null)
                            {
                                F_RXWU_Date2 = Convert.ToString(DynamData0[i]["FDATE2"]);
                            }
                            F_RXWU_Date = Convert.ToString(DynamData0[i]["FDATE1"]);
                            DBFEnteyID = Convert.ToString(DynamData0[i]["DBFEnteyID"]);
                            string json = "{\"Ids\":\"\",\"Numbers\":[],\"EntryIds\":\"" + DBFEnteyID + "\",\"RuleId\":\"TransferDirect-OUTSTOCK\",\"TargetBillTypeId\":\"\",\"TargetOrgId\":0,\"TargetFormId\":\"SAL_OUTSTOCK\",\"IsEnableDefaultRule\":\"false\",\"IsDraftWhenSaveFail\":\"false\",\"CustomParams\":{}}";
                            result = InvokeHelper.Push("STK_TransferDirect", json.ToString());
                            JObject jobject = JObject.Parse(result);
                            string a = Convert.ToString(jobject["Result"]["ResponseStatus"]["IsSuccess"]);
                            bool IsSuccess = a == "True";
                            if (IsSuccess)
                            {
                                string SQL3 = "/*dialect*/" + string.Format(@"update RXWU_t_Cust_Entry100013 set F_RXWU_COMBO='1' where FEntryId='{0}'", FEntryId);
                                DBUtils.Execute(ctx, SQL3);
                                Log.WriteLog("直接调拨单自动下推销售出库单成功", "报文:" + json.ToString() + "/r/n" + "返回结果:" + result.ToString() + "ctx:" + ctx);
                                var SuccessEntitysId = jobject["Result"]["ResponseStatus"]["SuccessEntitys"][0]["Id"];
                                if ((!F_RXWU_Date2.Equals("") && !F_RXWU_Date2.Equals("NULL") && !F_RXWU_Date2.Equals("0001-01-01 00:00:00")
                                    && !F_RXWU_Date2.Equals("1900-01-01 00:00:00")
                                    ) || (LX.Equals("补发订单")|| (LX.Equals("换货订单"))))
                                {
                                    string SQL4 = "/*dialect*/" + string.Format(@"update T_SAL_OUTSTOCK set FDATE='{1}' where FID='{0}'", SuccessEntitysId, F_RXWU_Date2);
                                    DBUtils.Execute(ctx, SQL4);
                                }
                                string Submit = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" + SuccessEntitysId + "\",\"SelectedPostId\":0,\"NetworkCtrl\":\"\",\"IgnoreInterationFlag\":\"\"}";
                                result = InvokeHelper.Submit("SAL_OUTSTOCK", Submit);
                                string Audit = "{\"CreateOrgId\":0,\"Numbers\":[],\"Ids\":\"" + SuccessEntitysId + "\",\"InterationFlags\":\"\",\"NetworkCtrl\":\"\",\"IsVerifyProcInst\":\"\",\"IgnoreInterationFlag\":\"\"}";
                                result = InvokeHelper.Audit("SAL_OUTSTOCK", Audit);
                            }
                            else
                            {
                                Log.WriteLog("直接调拨单自动下推销售出库单失败", "报文:" + json.ToString() + "/r/n" + "返回结果:" + result.ToString() + "ctx:" + ctx);
                                string SQL3 = "/*dialect*/" + string.Format(@"update RXWU_t_Cust_Entry100013 set F_RXWU_COMBO='0' where FEntryId='{0}'", FEntryId);
                                DBUtils.Execute(ctx, SQL3);
                            }
                        }
                    }
                }
                    }
            catch (Exception ex)
            {
                Log.WriteLog("直接调拨单自动下推销售出库单程序报错", "原因:" + ex.ToString() + "ctx:" + ctx);
            } 
        }
    }
}
